//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    hasUserInfo: false,//判断小程序的API，回调，参数，组件等是否在当前版本可用。
    isHide: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    // commentList: [
    //   {
    //     pic: '../../images/124134975.jpg',
    //     user: '黄八月',
    //     comment: '社区组织的志愿者活动，极大地调动了群众的积极性，一起动手保护环境，保护我们的环境。'
    //   },
    //   {
    //     pic: '../../images/124134975.jpg',
    //     user: '一生平安',
    //     comment: '旗委副书记韩亚成，旗人大常委会党组副书记、副主任马越伟到保康街道科尔沁社区慰问贫困党员和贫困群众，给群众带来了温暖。'
    //   },
    // ],
  },

  onLoad(){
    console.log(app.globalData.userInfo)
    wx.showLoading({
      title: '加载中...',
    })
    this.loadBanner()
    this.loadNews()
    this.loadComment()
  },

  onShow: function () {
    this.loadBanner()
    this.loadNews()
    this.loadComment()
  },

  onHide(){
    if(app.globalData.if_auth == 0){
      this.setData({
        isHide:false
      })
    }
  },

  getUserInfo: function (e) {
    // 用户点击允许
    console.log(e)
    console.log(app.globalData.if_auth)
    if (e.detail.userInfo) {
      this.setData({
        userInfo: e.detail.userInfo,
        hasUserInfo: true,
        isHide: false
      })
      let { encryptedData, iv, rawData, signature } = e.detail
      let access_token = wx.getStorageSync('access_token')
      wx.request({
        url: 'https://baokang.wshoto.com/wx/auth',
        method: 'POST',
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        data: {
          encryptedData: encryptedData,
          iv: iv,
          rawData: rawData,
          signature: signature,
          access_token: access_token
        },
        success: (res) => {
          console.log(res)
          wx.setStorageSync('userinfo', res.data.info)
          app.globalData.userInfo = e.detail.userInfo
          app.globalData.if_auth = 1
        }
      })
    } else {
      console.log('用户点击拒绝授权')
    }
  },

  toStreet() {
    if (app.globalData.if_auth == 0) {
      this.setData({
        isHide: true
      })
    } else {
      wx.navigateTo({
        url: '../situation/situation',
      })
    }
  },

  toPolicy() {
    if (app.globalData.if_auth == 0) {
      this.setData({
        isHide: true
      })
    } else {
      wx.navigateTo({
        url: '../policy/policy',
      })
    }
  },

  toGuide() {
    if (app.globalData.if_auth == 0) {
      this.setData({
        isHide: true
      })
    } else {
      wx.navigateTo({
        url: '../work-guide/work-guide',
      })
    }
  },

  moreShuoshi(){
    if (app.globalData.if_auth == 0) {
      this.setData({
        isHide: true
      })
    } else {
      wx.navigateTo({
        url: '../more-shuoshi/more-shuoshi',
      })
    }
  },

  morePingjia(){
    if (app.globalData.if_auth == 0) {
      this.setData({
        isHide: true
      })
    } else {
      wx.navigateTo({
        url: '../more-pingjia/more-pingjia',
      })
    }
  },

  toNotice(){
    if (app.globalData.if_auth == 0) {
      this.setData({
        isHide: true
      })
    } else {
      wx.navigateTo({
        url: '../notice/notice',
      })
    }
  },

  toNewsDetail(e){
    if (app.globalData.if_auth == 0) {
      this.setData({
        isHide: true
      })
    } else {
      let news_id = e.currentTarget.dataset.id
      wx.navigateTo({
        url: '../news-details/news-details?news_id=' + news_id,
      })
    }
  },

  // 轮播图
  loadBanner(){
    let access_token = wx.getStorageSync('access_token')
    wx.request({
      url: 'https://baokang.wshoto.com/wx/bannerlist',
      data:{
        access_token:access_token,
        location:'fuwu'
      },
      success:(res)=>{
        console.log(res.data)
        if(res.data.code >= 500){
          app.getLogin().then(function(){
            console.log('二次登录。。。')
          })
        }
        else if(res.data.code == 1){
          this.setData({
            bannerList:res.data.list,
            open_shuoshi:res.data.open_shuoshi
          })
        } 
      },
      complete:(res)=>{
        wx.hideLoading()
      }
    })
  },
  
  loadNews(){
    let access_token = wx.getStorageSync('access_token')
    wx.request({
      url: 'https://baokang.wshoto.com/wx/newslist',
      data:{
        access_token:access_token,
        location:'shuoshi',
        news_num:3
      },
      success:(res)=>{
        console.log(res)
        if(res.data.code == 1){
          this.setData({
            newsList:res.data.list
          })
        }
      }
    })
  },

  loadComment(){
    let access_token = wx.getStorageSync('access_token')
    wx.request({
      url: 'https://baokang.wshoto.com/wx/commentlist',
      data:{
        access_token:access_token,
        comment_num:3
      },
      success:(res)=>{
        console.log(res)
        if(res.data.code == 1){
          this.setData({
            commentList:res.data.list
          })
        }
      }
    })
  }
})
